#include"ArmorPatten.hpp"


/*
// 颜色判断
bool ArmorPatten::Color_Dcide(const Mat &image,const vector<Point> &contour,const Color &EnemyColor){// 待优化改成hsv
    int sum_r = 0 ,sum_b = 0;
    // int sum_ab = 0,sum_ar = 0;
    for(Point point : contour){
        // sum_ab = image.at<Vec3b>(point)[0] - image.at<Vec3b>(point)[2];
        // sum_ar = image.at<Vec3b>(point)[2] - image.at<Vec3b>(point)[0];

        sum_b += image.at<Vec3b>(point)[0]; // blue
        sum_r += image.at<Vec3b>(point)[2]; // red
    }
    cout<<"sum_b = " <<sum_b<<",  sum_r = "<<sum_r<<endl;
    // cout<<"sum_ab = " <<sum_ab<<",  sum_ar = "<<sum_ar<<endl;
    putText(image,"1: "+to_string(sum_b)+","+to_string(sum_r),contour[0]+Point(-10,-50),FONT_HERSHEY_SIMPLEX,0.7,Scalar(255,40,100),2);

    auto lightBarColor = sum_b > sum_r ? ArmorPatten::Color::blue : ArmorPatten::Color::red;
    if(lightBarColor != EnemyColor){
        return false;
    }else{
        return true;
    }
} 

*/







// 颜色判断
ArmorPatten::Color ArmorPatten::Color_Dcide(const Mat &image,const vector<Point> &contour,const Color &EnemyColor){// 待优化改成hsv
    int ColorCode = 0;
    ArmorPatten::Color enemycolor = EnemyColor;
    
    for(Point point : contour){
        // cout<<"红 - 蓝 = "<< image.at<Vec3b>(point)[2] - image.at<Vec3b>(point)[0] <<endl;
        if(enemycolor == Color::red && (image.at<Vec3b>(point)[2] - image.at<Vec3b>(point)[0] >= 110 )){// 筛选出纯度不高的红色
            ColorCode++;
            continue;
        }else if(enemycolor == Color::blue && ( image.at<Vec3b>(point)[0] - image.at<Vec3b>(point)[2] >= 110 )){// 筛选出纯度不高的蓝色
            ColorCode++;
            continue;
        }
        // 不合格在此输出：（调试用）
        // cout<<image.at<Vec3b>(point)[2] - image.at<Vec3b>(point)[0]<<endl; // 画面的二值化会影响这个值
    }
    // 看 合格的数量 ： 总点数  ,  合格率
    // cout <<"\n"<<ColorCode<<" : "<<contour.size()<<"  ratio_code = "<<(ColorCode * 1.0) / contour.size()<<endl;
    // putText(image,to_string(ColorCode)+":"+to_string(contour.size())+" , "+to_string((ColorCode * 1.0) / contour.size())+" %",contour[0]+Point(-10,-50),FONT_HERSHEY_SIMPLEX,0.7,Scalar(255,40,100),2);

    if((ColorCode * 1.0) / contour.size() < 0.65){ // 防止灯条颜色纯度有问题
        enemycolor = Color::black;
    }
    return enemycolor;
} 




/*

// 颜色判断
bool ArmorPatten::Color_Dcide(const Mat &image,const vector<Point> &contour,const Color &EnemyColor){// 待优化改成hsv
    int sum_r = 0 ,sum_b = 0 ;
    for(Point point : contour){
        sum_b += image.at<Vec3b>(point)[0]; // blue
        sum_r += image.at<Vec3b>(point)[2]; // red
    }
    cout<<"sum_b = " <<sum_b<<",  sum_r = "<<sum_r<<endl;
    auto lightBarColor = sum_b > sum_r ? ArmorPatten::Color::blue : ArmorPatten::Color::red;
    if(lightBarColor != EnemyColor){
        return false;
    }else{
        return true;
    }
} 

*/